package com.test.jpa.helloworld;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Date;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/10/16 21:23
 */
public class Main {
    public static void main(String[] args) {
        //1.创建EntityManagerFactory
        String persistenceUnitName = "jpa-1";
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);

        //2.创建EntityManager，类似于hibernate的SessionFactory
        EntityManager manager = factory.createEntityManager();

        //3.开启事务
        EntityTransaction transaction = manager.getTransaction();
        transaction.begin();

        //4.进行持久化操作
        Customer customer = new Customer();
        customer.setLastName("aaa");
        customer.setEmail("aaa@123.com");
        customer.setAge(23);
        customer.setBirth(new Date());
        customer.setCreateTime(new Date());
        manager.persist(customer);

        //5.提交事务
        transaction.commit();

        //6.关闭EntityManager
        manager.close();

        //7.关闭EntityManagerFactory
        factory.close();
    }
}
